package au.com.centrumsystems.hudson.plugin.buildpipeline;

import com.google.common.collect.Maps;

import java.util.Map;

public class ColumnForm<T extends CellForm<T>> {

    public static class Builder<T extends CellForm<T>> {
        private Map<Integer, T> cells;

        public Builder() {
            this.cells = Maps.<Integer, T>newHashMap();
        }

        public Builder add(int index, T cell) {
            this.cells.put(index, cell);
            return this;
        }

        public ColumnForm<T> build() {
            return new ColumnForm<T>(this.cells);
        }
    }

    private final Map<Integer,T> cells;

    ColumnForm(Map<Integer, T> cells) {
        this.cells = cells;
    }

    public int getHeight() {
        int height = 0;
        for (int index : cells.keySet()) {
            height = Math.max(height, index);
        }
        return height + 1;
    }

    public T get(int x) {
        return cells.get(x);
    }

    public boolean contains(int x) {
        return cells.containsKey(x);
    }
}
